<?php

function UnicodeEncode($str)
{
    preg_match_all('/./u', $str, $matches);
    $unicodeStr = "";
    foreach ($matches[0] as $m) {
        //修改
        //$unicodeStr .= "&#".base_convert(bin2hex(iconv('UTF‐8',"UCS‐4",$m)),16,10);
        $unicodeStr .= "\u" . base_convert(bin2hex(iconv('UTF-8', "UCS-4", $m)), 16, 16);
    }
    return $unicodeStr;
}

//
//$str = "新浪微博";
//echo UnicodeEncode($str);

function unicodeDecode($unicode_str)
{
    $json = '{"str":"' . $unicode_str . '"}';
    $arr = json_decode($json, true);
    if (empty($arr)) return '';
    return $arr['str'];
}

//$unicode_str = "\u65b0\u6d6a\u5fae\u535a";
//echo unicodeDecode($unicode_str);


function randStr2($length = 4)
{
    $str = '飞机扣发来的咖啡机热武器哦表学位网品牌区二层';
    $str = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $str);//剔除非uft8汉字
    $str = chunk_split($str, 3, ',');
    $arr = explode(',', $str);
    shuffle($arr);
    $str = implode($arr);
    $str = mb_substr($str, 0, $length, 'utf-8');
    return $str;
}
/*  验证码 */
/*$im = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
$green = imagecolorallocate($im, 0, 255, 0);
imagefill($im, 0, 0, $red);
//imageellipse($im,100,100,100,200,$blue);
$font = "C:\Windows\Fonts\simkai.ttf";
// 绘制干扰线
for ($i = 0; $i < 5; $i++) {
    $lineColor = imagecolorallocate($im, rand(50, 200), rand(50, 200), rand(50, 200));
    imageline($im, rand(0, 120), rand(0, 40), rand(0, 100), rand(0, 20), $lineColor);
}
// 绘制干扰点
for ($i = 0; $i < 100; $i++) {
    $pointColor = imagecolorallocate($im, rand(50, 200), rand(50, 200), rand(50, 200));
    imagesetpixel($im, rand(1, 119), rand(1, 39), $pointColor);
}
imagefttext($im,15,7,10,30,$blue,$font,randStr2());
header('Content-type:image/png');
imagepng($im);
imagedestroy($im);*/

//水印
/*$big=imagecreatefromjpeg('images/flower.jpg');
list($width,$height,$type)=getimagesize('images/flower.jpg');
$small=imagecreatetruecolor(200,100);
$blue=imagecolorallocate($small,0,0,255);
$red=imagecolorallocate($small,255,0,0);
imagefill($small,0,0,$red);
imageellipse($small,100,50,100,50,$blue);
imagecopymerge($big,$small,$width-200,$height-100,0,0,200,100,50);
header('Content-type:image/png');
imagepng($big);
imagedestroy($big);
imagedestroy($small);*/

//缩略图
//$big=imagecreatefromjpeg('images/flower.jpg');
//list($width,$height,$type)=getimagesize('images/flower.jpg');
//$small=imagecreatetruecolor(200,200);
//$red=imagecolorallocatealpha($small,255,0,0,127);
//imagealphablending($small,false);
//imagefill($small,0,0,$red);
//$rate=min(200/$width,200/$height);
//$rw=$width*$rate;
//$rh=$height*$rate;
//imagecopyresampled($small,$big,(200-$rw)/2,(200-$rh)/2,0,0,$rw,$rh,$width,$height);
//imagesavealpha($small,true);
//header('content-type:image/png');
//imagepng($small);
//imagedestroy($big);
//imagedestroy($small);

header('Access-Control-Allow-Origin:*');
echo 12;